package edu.bkcloud08.portal.controllers;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import edu.bkcloud08.portal.models.Lab;
import edu.bkcloud08.portal.models.Subject;
import edu.bkcloud08.portal.services.DatabaseService;
import edu.bkcloud08.portal.services.DatabaseServiceImpl;
@Controller
@SessionAttributes("subject")
public class SubjectsController {
	private List<Subject> subjects = new ArrayList<Subject>();
	private DatabaseService dbservice = new DatabaseServiceImpl();

	@RequestMapping(value = "/subjects", method = RequestMethod.GET)
	public String listSubjects(Locale locale, Model model) {
		System.out.println("List subjects");

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// HttpServletResponse response = ServletContextAttributeExporter
		if (subjects.isEmpty()) {
//			Connection conn = dbservice.getConnection(DBInfo.dbhost,
//					DBInfo.dbport, DBInfo.database, DBInfo.dbuser,
//					DBInfo.dbpass);
//			Statement stmt;
//			if (conn != null) {
//				try {
//					stmt = conn.createStatement();
//					ResultSet rs = stmt
//							.executeQuery("select id,name,description from res_subjects");
//					while (rs.next()) {
//						subjects.add(new Subject(rs.getInt(1), rs.getString(2),
//								rs.getString(3)));
//					}
//
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
			List<Lab> sub1_labs = new LinkedList<Lab>() ;
			sub1_labs.add(new Lab(1,"Lab1","Lam quen voi he dieu hanh"));
			sub1_labs.add(new Lab(2,"Lab2","Mot vai lenh co ban trong Linux"));
			sub1_labs.add(new Lab(3,"Lab3","Lap trinh IPC"));
			sub1_labs.add(new Lab(4,"Lab4","Cong cu makefile"));			
			
			
			List<Lab> sub2_labs = new LinkedList<Lab>() ;
			sub2_labs.add(new Lab(5,"Lab1","Lam quen voi TCP/IP va Wireshark")) ;
			sub2_labs.add(new Lab(6,"Lab2","Ethernet,ARP,ICMP"));
			sub2_labs.add(new Lab(7,"Lab3","Lam quen voi cac thiet bi mang"));
			sub2_labs.add(new Lab(8,"Lab4","Packet tracer"));
			sub2_labs.add(new Lab(9,"Lab5","Su dung Wireshark"));
			
			subjects.add(new Subject(503001,"Computer Network 1", "Mang may tinh 1",sub1_labs));
			subjects.add(new Subject(506001,"Operating System", "He dieu hanh",sub2_labs));

		}
		model.addAttribute("subjects", subjects);
		return "subjects";
	}

	@RequestMapping(value = "/subject", method = RequestMethod.GET)
	@ModelAttribute("subject")
	public Subject subject(@RequestParam("id") int id, Model model) {
		Subject re = null;
		for (Subject a : subjects) {
			if (a.getId() == id) {
				re = a;
				break;
			}
		}
		model.addAttribute("labs", re.getLabs());
		return re;
	}
	
	@RequestMapping(value = "/lab", method = RequestMethod.GET)
	public String lab(@RequestParam("subid") int id, @RequestParam("labid") int labid, Model model) {
		Subject sub  =  subject(id,model) ;
		List<Lab> labs  = sub.getLabs() ;
		Lab lab = null;
		for(Lab a : labs) {
			if(a.getId() ==  labid) {
				lab = a;
				break ;
			}
		}
		model.addAttribute("subject",sub) ;
		model.addAttribute("lab",lab) ;
		return "lab" ;
	}
	
	
	@RequestMapping(value = "/add-subject", method = RequestMethod.GET)
	public String showAddSubjectForm(Locale locale, Model model) {
		System.out.println("page : subject -> add-subject");
		model.addAttribute("subject", new Subject()) ;
		return "add-subject";
	}
	
	@RequestMapping(value = "/add-subject", method = RequestMethod.POST)	
	public String onSubmitAddSubjectForm(@ModelAttribute("subject") Subject subject,BindingResult result, Locale locale,Model model) {
		System.out.println("process form add subject" );
		
		return "redirect:add-subject-successful" ;
	}
}
